import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
//import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_oa/beans/news_bean.dart';
import 'package:flutter_oa/view/loading.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';

class Page2 extends StatefulWidget {
  @override
  _Page2State createState() => _Page2State();
}

class _Page2State extends State<Page2> {
  var loadingDialog;
  var content = '12345';

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(10.0),
      child: SingleChildScrollView(
//        child: Html(
//          data: content,
//        ),
      child: HtmlWidget(
        content,
        webView: true,
      ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
//    _showDialog('wwww');
    _getHttp();
  }
  _showDialog(String text) {
    loadingDialog = LoadingDialog(
      text: text,
    );
    showDialog<Null>(
        context: context,
        barrierDismissible: false, //外部触摸
        builder: (BuildContext context) {
          return loadingDialog;
        });
  }

  //https://www.hbjt.org.cn/hbjt/api/article/articleInfo?articleId=63113cb7ebf94b8dabbe5fb593dba8db
  void _getHttp() async {
    Map data = new Map<String, dynamic>();
    data["articleId"] = '63113cb7ebf94b8dabbe5fb593dba8db';
    try {
      // flutter packages pub run build_runner build
      Dio dio = new Dio();
      dio.options.responseType = ResponseType.plain; //转字符串
      Response response = await dio.get(
          'https://www.hbjt.org.cn/hbjt/api/article/articleInfo',
          queryParameters: data);
      if (loadingDialog != null) {
        loadingDialog = null;
        Navigator.pop(context);
      }

      Map<String, dynamic> decoded = json.decode(response.toString());
      NewsBean newsBean = NewsBean.fromJson(decoded);
      setState(() {
        var data = newsBean.data;
        setState(() {
          content = data.content;
        });
      });
    } catch (e) {
      print(e);
    }
  }
}
